package com.liuda.workbench.controller;

import com.liuda.commons.constants.Constants;
import com.liuda.commons.domain.ReturnObject;
import com.liuda.commons.utils.DateUtil;
import com.liuda.commons.utils.UUIDUtil;
import com.liuda.settings.domain.DicValue;
import com.liuda.settings.domain.User;
import com.liuda.settings.service.DicValueService;
import com.liuda.settings.service.UserService;
import com.liuda.workbench.domain.TranHistory;
import com.liuda.workbench.domain.Transfer;
import com.liuda.workbench.domain.TransferRemark;
import com.liuda.workbench.mapper.TransferRemarkMapper;
import com.liuda.workbench.service.CustomerService;
import com.liuda.workbench.service.TranHistoryService;
import com.liuda.workbench.service.TransferRemarkService;
import com.liuda.workbench.service.TransferService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
public class TransactionController {
    @Autowired
    DicValueService dicValueService;
    @Autowired
    UserService userService;
    @Autowired
    CustomerService customerService;
    @Autowired
    TransferService transferService;
    @Autowired
    TransferRemarkService transferRemarkService;
    @Autowired
    TranHistoryService tranHistoryService;
    @RequestMapping("/workbench/transaction/toIndex.do")
    public String toIndex(HttpServletRequest request){
        List<DicValue> stageList=dicValueService.queryDicValueByTypeCode("stage");
        List<DicValue> typeList=dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        List<User> userList=userService.getAllUnlockedUsers();
        List<Transfer> transactionList=transferService.selectAllForDetail();
        request.setAttribute("stageList",stageList);
        request.setAttribute("typeList",typeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("userList",userList);
        request.setAttribute("transactionList",transactionList);
        return "workbench/transaction/index";
    }
    @RequestMapping("/workbench/transaction/toCreate.do")
    public String toCreate(HttpServletRequest request){
        List<DicValue> stageList=dicValueService.queryDicValueByTypeCode("stage");
        List<DicValue> typeList=dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        List<User> userList=userService.getAllUnlockedUsers();
        request.setAttribute("stageList",stageList);
        request.setAttribute("typeList",typeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("userList",userList);
        return "workbench/transaction/save";
    }
    @RequestMapping("/workbench/transaction/toEdit.do")
    public String toEdit(String id,HttpServletRequest request){
        List<DicValue> stageList=dicValueService.queryDicValueByTypeCode("stage");
        List<DicValue> typeList=dicValueService.queryDicValueByTypeCode("transactionType");
        List<DicValue> sourceList=dicValueService.queryDicValueByTypeCode("source");
        List<User> userList=userService.getAllUnlockedUsers();
        Transfer transaction=transferService.selectForDetailById(id);
        request.setAttribute("stageList",stageList);
        request.setAttribute("typeList",typeList);
        request.setAttribute("sourceList",sourceList);
        request.setAttribute("userList",userList);
        request.setAttribute("transaction",transaction);
        return "workbench/transaction/edit";
    }
    @RequestMapping("/workbench/transaction/toDetail.do")
    public String toDetail(String tranId,HttpServletRequest request){
        Transfer transaction=transferService.selectForDetailById(tranId);
        ResourceBundle bundle=ResourceBundle.getBundle("possibility");
        transaction.setPossibility(bundle.getString(transaction.getStage()));
        List<DicValue> stageList=dicValueService.queryDicValueByTypeCode("stage");
        List<TransferRemark> remarkList=transferRemarkService.selectByTranId(tranId);
        List<TranHistory> tranHistoryList=tranHistoryService.selectByTranId(tranId);
        request.setAttribute("stageList",stageList);
        request.setAttribute("transaction",transaction);
        request.setAttribute("remarkList",remarkList);
        request.setAttribute("tranHistoryList",tranHistoryList);
        return "workbench/transaction/detail";
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/getPossibilityByStage")
    public Object getPossibilityByStage(String stage){
        ResourceBundle bundle=ResourceBundle.getBundle("possibility");
        return bundle.getString(stage);
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/queryCustomerNameByLike.do")
    public Object queryCustomerNameByLike(String name){
       return customerService.queryCustomerNameByLike(name);
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/saveCreate.do")
    public Object saveCreate(@RequestParam Map map, HttpSession session){
        User user=(User) session.getAttribute(Constants.SESSION_USER);
        map.put("user",user);
        ReturnObject object=new ReturnObject();
        try {
            int count= transferService.insertSelective(map);
            if(count<1){
                object.setState(Constants.RETURN_OBJECT_FAILURE);
                object.setMsg("系统忙，请稍后重试...");
            }else{
                object.setState(Constants.RETURN_OBJECT_SUCCESS);
                object.setMsg("创建成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/saveEdit.do")
    public Object saveEdit(@RequestParam Map map, HttpSession session){
        User user=(User) session.getAttribute(Constants.SESSION_USER);
        map.put("user",user);
        ReturnObject object=new ReturnObject();
        try {
            int count= transferService.updateSelective(map);
            if(count<1){
                object.setState(Constants.RETURN_OBJECT_FAILURE);
                object.setMsg("系统忙，请稍后重试...");
            }else{
                object.setState(Constants.RETURN_OBJECT_SUCCESS);
                object.setMsg("修改成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/deleteSelected.do")
    public Object deleteSelected(String[] ids){
        ReturnObject object=new ReturnObject();
        try {
            int count= transferService.deleteSelected(ids);
            if(count<1){
                object.setState(Constants.RETURN_OBJECT_FAILURE);
                object.setMsg("系统忙，请稍后重试...");
            }else{
                object.setState(Constants.RETURN_OBJECT_SUCCESS);
                object.setMsg("删除成功！");
            }
        }catch (Exception e){
            e.printStackTrace();
            object.setState(Constants.RETURN_OBJECT_FAILURE);
            object.setMsg("系统忙，请稍后重试...");
        }
        return object;
    }
    @ResponseBody
    @RequestMapping("/workbench/transaction/getOrderNo.do")
    public Object getOrderNo(String value){
        return dicValueService.getOrderNoByValue(value);
    }
}
